package com.clj.util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

/**
 * Author: Harry Chen
 * Date: 2023/02/24 15:47
 * Description: 进行数据库的连接
 */
public class DBUtil {
    private static String DRIVER = "com.mysql.cj.jdbc.Driver";
    private static String USER_NAME = "root";
    private static String PWD = "000000";
    private static String URL = "jdbc:mysql://43.138.107.193:3306/学生成绩管理系统_陈林杰?serverTimezone=UTC&characterEncoding=utf-8";


    static {
        try {
            Class.forName(DRIVER);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    public static Connection getConn() {
        try {
            return DriverManager.getConnection(URL, USER_NAME, PWD);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static void closeConn(Connection connection) {
        if (connection != null) {
            try {
                connection.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public static void closePs(PreparedStatement ps) {
        if (ps != null) {
            try {
                ps.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public static void closeRs(ResultSet rs) {
        if (rs != null) {
            try {
                rs.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
